package com.bflow.web.controller;

import com.bflow.api.IBflowDefineActService;
import com.bflow.api.IBflowRunRecordService;
import com.bflow.api.bo.BflowDefineActBo;
import com.bflow.api.bo.BflowDefineActQueryParam;
import com.bflow.api.bo.BflowRunRecordBo;
import com.bflow.api.bo.BflowRunRecordQueryParam;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.consts.CommConst;
import com.bflow.util.JsonUtil;
import com.bflow.web.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Name 审批记录
 * @Author czy
 * @Date 2018-12-17
 */
@Controller
@RequestMapping(value = "/bflow/record")
public class BflowRecordController extends BflowBaseController {

    @Autowired
    private IBflowRunRecordService<?, BflowRunRecordBo> bflowRunRecordService;

    @RequestMapping(value = "list")
    @ResponseBody
    public String list(BflowRunRecordQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            queryParam.setDefParam(1, 500, "create_time", "DESC");
            PageResult<BflowRunRecordBo> pageResult = bflowRunRecordService.findPage(queryParam);
            return JsonUtil.toStr(pageResult.getResult());
        } catch (Exception e) {
            return returnFail(e, "列表查询失败");
        }
    }

    @RequestMapping(value = "get")
    @ResponseBody
    public String get(@RequestParam String id, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowRunRecordBo bo = bflowRunRecordService.findById(id);
            return returnOk(bo, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public String save(BflowRunRecordBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
           // bflowRunRecordService.save(bo);
            return returnOk("保存成功", "保存成功");
        } catch (Exception e) {
            return returnFail(e, "保存失败");
        }
    }

    @RequestMapping(value = "del")
    @ResponseBody
    public String del(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<String> ids = RequestUtil.getStrParameterList(req, "selectVals");
            Object[] objects = ids.toArray();
            String[] newIds= new String[ids.size()];
            for (int i=0;i<objects.length;i++){
//                newIds[i]=(String)objects[i];
            }
            bflowRunRecordService.deleteByIds(newIds);
            return returnOk("删除成功", "删除成功");
        } catch (Exception e) {
            return returnFail(e, "删除失败");
        }
    }

    @RequestMapping(value = "edit")
    @ResponseBody
    public String update(BflowRunRecordBo bo,HttpServletRequest req, HttpServletResponse resp) {
        try {
            bflowRunRecordService.update(bo);
            return returnOk("修改成功", "修改成功");
        } catch (Exception e) {
            return returnFail(e, "修改失败");
        }
    }


}
